package com.neuronbit.businessflow.intellij.editor.swing.editor.propertyInspector.properties;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.util.Computable;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiMethod;
import com.intellij.psi.PsiParameter;
import com.neuronbit.businessflow.intellij.editor.UIDesignerBundle;
import com.neuronbit.businessflow.intellij.editor.swing.editor.FormEditingUtil;
import com.neuronbit.businessflow.intellij.editor.swing.editor.RadStatefulTaskComponent;
import com.neuronbit.businessflow.intellij.editor.swing.editor.propertyInspector.Property;
import com.neuronbit.businessflow.intellij.editor.swing.editor.propertyInspector.PropertyEditor;
import com.neuronbit.businessflow.intellij.editor.swing.editor.propertyInspector.PropertyRenderer;
import com.neuronbit.businessflow.intellij.editor.swing.editor.propertyInspector.renderers.LabelPropertyRenderer;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * The type Method params property.
 */
public class StateCheckerMethodParamsProperty extends Property<RadStatefulTaskComponent, String> {

    /**
     * Instantiates a new Method params property.
     *
     * @param component the component
     * @param name      the name
     */
    public StateCheckerMethodParamsProperty(RadStatefulTaskComponent component, String name) {
        super(null, name);
    }

    @Override
    public String getValue(RadStatefulTaskComponent component) {
        return "";
    }

    @Override
    protected void setValueImpl(RadStatefulTaskComponent component, String value) throws Exception {

    }

    @Override
    public @NotNull PropertyRenderer getRenderer() {
        return new LabelPropertyRenderer("");
    }

    @Override
    public @Nullable PropertyEditor<String> getEditor() {
        return null;
    }

    @Override
    public Property @NotNull [] getChildren(RadStatefulTaskComponent component) {
        return ApplicationManager.getApplication().runReadAction((Computable<Property[]>) () -> {
            if (component.getStateCheckerClassToBind() == null) {
                return EMPTY_ARRAY;
            }
            PsiClass clazz = FormEditingUtil.findClassToBind(component.getModule(), component.getStateCheckerClassToBind());
            if (clazz == null) {
                return EMPTY_ARRAY;
            }
            String methodToBind = component.getStateCheckerMethodToBind();
            String methodName = StringUtils.substringBefore(methodToBind, "(");
            PsiMethod[] psiMethods = clazz.findMethodsByName(methodName, true);
            if (ArrayUtils.isNotEmpty(psiMethods)) {
                for (PsiMethod psiMethod : psiMethods) {
                    //String s = ApplicationManager.getApplication().runReadAction((Computable<String>) () -> PsiUtils.formatMethod(psiMethod));
                    //if (s.equals(methodToBind)) {
                    //TODO shunyun 2024/4/2: 这里还需要匹配参数的个数，如果有多个参数个数一样的重载方法，则需要把可以接受的参数类型也一并拿出，
                    // 但，用户才选择方法的时候，其实已经明确了是使用哪一个重载方法，可以在选择页面，给一个匹配到的重载方法预览
                    if (psiMethod.getParameterList().isEmpty()) {
                        return EMPTY_ARRAY;
                    }

                    PsiParameter[] parameters = psiMethod.getParameterList().getParameters();
                    Property[] properties = new Property[parameters.length];
                    for (int i = 0; i < parameters.length; i++) {
                        properties[i] = new StateCheckerMethodParamProperty(this, component.getProject(), parameters[i], String.valueOf(i));
                    }

                    return properties;
                    //}
                }

            }

            return EMPTY_ARRAY;
        });

    }
//
//    @Override
//    public boolean needRefreshPropertyList() {
//        return true;
//    }


}
